package com.fr.report.js;

import com.fr.base.CodeUtils;
import com.fr.base.DefaultValues;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.TemplateWorkBook;
import com.fr.report.cellElement.Formula;
import com.fr.report.parameter.Parameter;
import com.fr.util.Utils;
import com.fr.web.Repository;
import com.fr.web.core.SessionIDInfor;
import java.text.DateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ReportletHyperlink extends AbstractHyperlink
{
  private String reportPath;
  private String postMethod;
  private String getMethod;
  private boolean extendParameters;
  private boolean showParameterInterface;
  private boolean byPost;

  public ReportletHyperlink()
  {
    this.reportPath = "";
    this.postMethod = "FR.doHyperlinkByPost";
    this.getMethod = "FR.doHyperlinkByGet";
    this.extendParameters = false;
    this.showParameterInterface = false;
    this.byPost = false;
  }

  public ReportletHyperlink(String paramString)
  {
    this(paramString, "");
  }

  public ReportletHyperlink(String paramString1, String paramString2)
  {
    this.reportPath = "";
    this.postMethod = "FR.doHyperlinkByPost";
    this.getMethod = "FR.doHyperlinkByGet";
    this.extendParameters = false;
    this.showParameterInterface = false;
    this.byPost = false;
    setReportletPath(paramString1);
    setTargetFrame(paramString2);
  }

  public String getReportletPath()
  {
    return this.reportPath;
  }

  public void setReportletPath(String paramString)
  {
    this.reportPath = paramString;
  }

  public boolean isExtendParameters()
  {
    return this.extendParameters;
  }

  public void setExtendParameters(boolean paramBoolean)
  {
    this.extendParameters = paramBoolean;
  }

  public boolean isShowParameterInterface()
  {
    return this.showParameterInterface;
  }

  public void setShowParameterInterface(boolean paramBoolean)
  {
    this.showParameterInterface = paramBoolean;
  }

  public boolean isByPost()
  {
    return this.byPost;
  }

  public void setByPost(boolean paramBoolean)
  {
    this.byPost = paramBoolean;
  }

  public String createURL(Repository paramRepository)
  {
    try
    {
      return createReportPath(paramRepository);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return null;
  }

  private String createReportPath(Repository paramRepository)
    throws Exception
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if (this.byPost)
      localStringBuffer.append(this.postMethod);
    else
      localStringBuffer.append(this.getMethod);
    localStringBuffer.append("(\"");
    localStringBuffer.append(paramRepository.checkoutReportletPath(getReportletPath()));
    localStringBuffer.append("\", ");
    localStringBuffer.append(createParaPath(paramRepository));
    localStringBuffer.append(", \"");
    localStringBuffer.append(getTargetFrame());
    localStringBuffer.append("\", \"");
    localStringBuffer.append(features4NewWindow());
    localStringBuffer.append("\")");
    return localStringBuffer.toString();
  }

  private JSONObject createParaPath(Repository paramRepository)
    throws Exception
  {
    int i;
    Object localObject1;
    Object localObject3;
    JSONObject localJSONObject = new JSONObject();
    if (this.parameters != null)
      for (i = 0; i < this.parameters.length; ++i)
      {
        localObject1 = this.parameters[i];
        Object localObject2 = ((Parameter)localObject1).getValue();
        String str = "";
        if (localObject2 != null)
        {
          if (localObject2 instanceof Formula)
            localObject2 = ((Formula)localObject2).getResult();
          if (localObject2 == null)
            break label153:
          if (localObject2 instanceof Date)
          {
            localObject3 = FRContext.getDefaultValues();
            str = ((DefaultValues)localObject3).getDateTimeFormat().format(localObject2);
          }
          else if (localObject2 instanceof Double)
          {
            str = Utils.doubleToString(((Double)localObject2).doubleValue());
          }
          else
          {
            str = localObject2.toString();
          }
        }
        localJSONObject.put(CodeUtils.cjkEncode(((Parameter)localObject1).getName()), CodeUtils.cjkEncode(str));
      }
    label153: localJSONObject.put("__pi__", this.showParameterInterface);
    if (isExtendParameters())
    {
      Map localMap = paramRepository.getSessionIDInfor().getParameterMap4Execute();
      if (localMap != null)
      {
        localObject1 = (paramRepository.getSessionIDInfor().getWorkBookDefine() == null) ? new Parameter[0] : paramRepository.getSessionIDInfor().getWorkBookDefine().getParameters();
        int j = 0;
        int k = localObject1.length;
        while (j < k)
        {
          if (localMap.get(localObject1[j].getName()) == null)
          {
            localObject3 = localObject1[j].getValue();
            if (localObject3 instanceof Formula)
              localObject3 = ((Formula)localObject1[j].getValue()).getResult();
            localMap.put(localObject1[j].getName(), localObject3);
          }
          ++j;
        }
        Iterator localIterator = localMap.entrySet().iterator();
        while (localIterator.hasNext())
        {
          Map.Entry localEntry = (Map.Entry)localIterator.next();
          localJSONObject.put(CodeUtils.cjkEncode((String)localEntry.getKey()), CodeUtils.cjkEncode("" + localEntry.getValue()));
        }
      }
    }
    return ((JSONObject)(JSONObject)localJSONObject);
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("JavaScript").attr("class", getClass().getName());
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("ReportletName");
    if (isExtendParameters())
      paramXMLPrintWriter.attr("extendParameters", isExtendParameters());
    if (isShowParameterInterface())
      paramXMLPrintWriter.attr("showPI", isShowParameterInterface());
    if (this.byPost)
      paramXMLPrintWriter.attr("byPost", this.byPost);
    paramXMLPrintWriter.textNode(getReportletPath());
    paramXMLPrintWriter.end();
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("ReportletName")))
    {
      String str;
      if (StringUtils.isNotBlank(str = paramXMLableReader.getAttr("extendParameters")))
        setExtendParameters(Boolean.valueOf(str).booleanValue());
      if (StringUtils.isNotBlank(str = paramXMLableReader.getAttr("showPI")))
        setShowParameterInterface(Boolean.valueOf(str).booleanValue());
      if (StringUtils.isNotBlank(str = paramXMLableReader.getAttr("byPost")))
        setByPost(Boolean.valueOf(str).booleanValue());
      if (StringUtils.isNotBlank(str = paramXMLableReader.getElementValue()))
        setReportletPath(str);
    }
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
      return true;
    if (!(paramObject instanceof ReportletHyperlink))
      return false;
    return ((super.equals(paramObject)) && (this.extendParameters == ((ReportletHyperlink)paramObject).extendParameters) && (ComparatorUtils.equals(this.reportPath, ((ReportletHyperlink)paramObject).reportPath)) && (this.byPost == ((ReportletHyperlink)paramObject).byPost));
  }
}